import 'package:cjybase/delegate/cupertino_docalizations_delegate.dart';
import 'package:cjybase/entity/screen_default.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

abstract class MainBase extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ScreenDefault screenDefault = setDefaultWH();
    initData();
    return ScreenUtilInit(
      designSize: Size(screenDefault.width, screenDefault.height),
      builder: () => MaterialApp(
        localizationsDelegates: [
          // 本地处理国际化
          CupertinoLocalizationsDelegate(),
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: [
          const Locale('zh', 'CN'),
          const Locale('en', 'US'),
        ],
        title: setTitle(),
        //注册路由表
        initialRoute: '/',
        routes: {
          '/': (context) => setStartPage(context),
        },
        builder: (BuildContext context, Widget child) {
          /// make sure that loading can be displayed in front of all other widgets
          Widget page = setPageContent(context, child);
          return FlutterEasyLoading(
            child: MediaQuery(
              ///设置文字大小不随系统设置改变
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: page == null ? child : page,
            ),
          );
        },
      ),
    );
  }

  /// 设置全局页面，child也要加入到页面中
  Widget setPageContent(BuildContext context, Widget child) {
    return null;
  }

  /// APP启动进入的页面
  Widget setStartPage(BuildContext context);

  /// APP标题
  String setTitle();

  /// 设置UI设计稿尺寸
  ScreenDefault setDefaultWH();

  /// 初始化数据
  initData();
}
